﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsGame1.gamePlugins.renderer;
using Cookie_Hero.events;
using Cookie_Hero.graphics;
using Microsoft.Xna.Framework.Content;
using Cookie_Hero.communications;
using Cookie_Hero.testing;
using Microsoft.Xna.Framework;
using WindowsGame1;
using Cookie_Hero.events.types;

namespace Cookie_Hero
{
    public class GameContainer
    {
        public List<EventArgument> stageVariables { get; private set; }
        public List<PhysicsObject> objects { get; private set; }
        public List<EventHolder> stageEvents { get; private set; }
        public ImageSequenceController animations { get; private set; }
        public Database db { get; private set; }
        public int initstate = 0;

        public GameContainer(ContentManager Content)
        {
            db = new Database();
            stageVariables = new List<EventArgument>();
            animations = new ImageSequenceController(Content);
            animations = db.GetAllAnimations(animations);
        }

        public void initLevel(int level)
        {
            objects = db.GetAllObjectsFromLevel(level);
            stageEvents = db.GetAllEventsFromLevel(level);
            initstate = 1;
        }

        public void update(GameTime gameTime, Physics physics, GameDisplay gameDisplay)
        {
            updateEvents();
            animations.update((long)gameTime.ElapsedGameTime.TotalMilliseconds);
            objects = physics.renderframe(objects, gameTime, animations, gameDisplay.getViewPort());
            initstate = 0;
        }

        private void updateEvents()
        {
            foreach(EventHolder eventholder in stageEvents)
            {
                int inputresult = 0;
                List<EventRow> eventrows_output = new List<EventRow>();

                foreach(EventRow eventrow in eventholder.eventrows)
                {
                    // alle eventlisteners
                    switch(eventrow.type.type)
                    {
                        case "IKeyboardPress": 
                            if(IKeyboard.IsTrue(eventrow, this))
                            {
                                inputresult++;
                            }
                        break;
                        case "IMouseClick": 
                            if(IMouseClick.IsTrue(eventrow, this))
                            {
                                inputresult++;
                            }
                        break;
                        case "ICollision": 
                            if(ICollision.IsTrue(eventrow, this))
                            {
                                inputresult++;
                            }
                        break;
                        case "IEnterFrame": 
                            if(IEnterFrame.IsTrue(eventrow, this))
                            {
                                inputresult++;
                            }
                        break;
                        default:
                            eventrows_output.Add(eventrow);
                        break;
                    }

                }


                if (inputresult != 0 && inputresult == eventholder.eventrows.Count - eventrows_output.Count)
                {
                    Console.WriteLine(eventholder.name + " getriggert");
                    foreach (EventRow eventrow_out in eventrows_output)
                    {
                        // alle events
                        switch (eventrow_out.type.type)
                        {
                            case "OMove":
                                OMove.Update(eventrow_out, this);
                                break;
                            case "OPosition":
                                OPosition.Update(eventrow_out, this);
                                break;
                            case "OChangeAnimation":
                                OChangeAnimation.Update(eventrow_out, this);
                                break;
                            case "OVariable":
                                OVariable.Update(eventrow_out, this);
                                break;
                        }
                    }
                }
            }
        }


        public void StartLevel(int levelid)
        {
            initLevel(levelid);
        }
    }
}
